﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcVehicleInsurance.Models;

namespace MvcVehicleInsurance.Areas.Admin.Controllers
{
    public class ClaimController : Controller
    {

        private DataClassesVehicleDataContext data = new DataClassesVehicleDataContext();
        //
        // GET: /Admin/Claim/

        public ActionResult Index()
        {
            List<ClaimDetail> claim = (from cl in data.ClaimDetails select cl).ToList();
            if (ModelState.IsValid)
            {
                return View(claim);
            }
            return View();
        }

        //
        // GET: /Admin/Claim/Details/5

        public ActionResult Details(int id)
        {
            ClaimDetail claim = (data.ClaimDetails.Where(cl => cl.claimNumber == id)).First();
            if (ModelState.IsValid)
            {
                return View(claim);
            }
            return View();
        }

        //
        // GET: /Admin/Claim/Create

        public ActionResult Create()
        {
            List<CustomerBillingInformation> listCus = (from Cus in data.CustomerBillingInformations select Cus).ToList();
            ViewData["listBill"] = new SelectList(listCus, "billNo", "date");
            ClaimDetail claim = new ClaimDetail();

            return View(claim);
        }

        //
        // POST: /Admin/Claim/Create

        [HttpPost]
        public ActionResult Create(ClaimDetail claim)
        {
            if (ModelState.IsValid)
            {
                List<CustomerBillingInformation> listCus = (from Cus in data.CustomerBillingInformations select Cus).ToList();
                ViewData["listBill"] = new SelectList(listCus, "billNo", "date");
                ClaimDetail cla = new ClaimDetail();
                cla.billNo = claim.billNo;
                cla.policyStarDate = claim.policyStarDate;
                cla.policyEndDate = claim.policyEndDate;
                cla.placeOfAccident = claim.placeOfAccident;
                cla.dateOfAccident = claim.dateOfAccident;
                cla.insuredAmount = claim.insuredAmount;
                cla.claimableAmount = claim.claimableAmount;

                data.ClaimDetails.InsertOnSubmit(claim);
                data.SubmitChanges();
                RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Admin/Claim/Edit/5

        public ActionResult Edit(int id)
        {
            ClaimDetail claim = new ClaimDetail();
            return View();
        }

        //
        // POST: /Admin/Claim/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, ClaimDetail claim)
        {
            if (ModelState.IsValid)
            {
                ClaimDetail cla = (data.ClaimDetails.Where(cl => cl.claimNumber == id)).First();
                cla.billNo = claim.billNo;
                cla.policyStarDate = claim.policyStarDate;
                cla.policyEndDate = claim.policyEndDate;
                cla.placeOfAccident = claim.placeOfAccident;
                cla.dateOfAccident = claim.dateOfAccident;
                cla.insuredAmount = claim.insuredAmount;
                cla.claimableAmount = claim.claimableAmount;

                data.SubmitChanges();
                RedirectToAction("Index");
            }

            return View();

        }

    }
}
